package com.jinkaimen.kqb.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.ui.widget.LoadingLayout;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.NetUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.NotifyListBean;
import com.jinkaimen.kqb.bean.hss.PublicNewsBean;
import com.jinkaimen.kqb.constant.Constants;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.ui.activity.addmerchant.WebViewActivity;
import com.jinkaimen.kqb.ui.adapter.NotifyNewAdapter;
import com.jinkaimen.kqb.ui.adapter.PublicNewsAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import retrofit2.Call;
import retrofit2.Response;


public class NotifyFragment extends BaseFragment {
    private RelativeLayout rl_underway;
    private TextView tv_title11;
    private TextView iv_shen_he_zhong;
    private TextView tv_title21;
    private TextView iv_yi_shen_he;
    private RelativeLayout rl_over;
    private ListView listView;
    private SmartRefreshLayout pLvMemberManager;
    private LoadingLayout loadingLayout;
    private NotifyNewAdapter notifyAdapter;
    private PublicNewsAdapter publicNewsAdapter;
    private List<NotifyListBean.ListBean> notifyList;
    private List<PublicNewsBean.RecordsBean> publicNewsList;
    private int currentPage;
    private boolean isPublicNews = true;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {

    }

    @Override
    protected View initContentView() {
        return LayoutInflater.from(mContext).inflate(R.layout.frag_notify, null);
    }

    @Override
    protected void initChildView() {
        rl_underway = findViewById(R.id.rl_underway);
        tv_title11 = findViewById(R.id.tv_title1);
        iv_shen_he_zhong = findViewById(R.id.iv_shen_he_zhong);
        tv_title21 = findViewById(R.id.tv_title2);
        iv_yi_shen_he = findViewById(R.id.iv_yi_shen_he);
        rl_over = findViewById(R.id.rl_over);
        listView = findViewById(R.id.lv_frag_notify);
        pLvMemberManager = findViewById(R.id.plv_activity_member_manager);
        pLvMemberManager.setRefreshFooter(new ClassicsFooter(mContext).setSpinnerStyle(SpinnerStyle.Scale));
        loadingLayout = findViewById(R.id.loading_layout);
        loadingLayout.setStatus(LoadingLayout.Success);
        notifyList = new ArrayList<>();
        publicNewsList = new ArrayList<>();
        notifyAdapter = new NotifyNewAdapter(mContext, notifyList);
        publicNewsAdapter = new PublicNewsAdapter(mContext, publicNewsList);
        listView.setAdapter(publicNewsAdapter);
        currentPage = 1;
        netWork();
        loadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadingLayout.setStatus(LoadingLayout.Loading);
                currentPage = 1;
                if (isPublicNews) {
                    netWork();
                } else {
                    netWorkNotify();
                }

            }
        });
        pLvMemberManager.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                currentPage = 1;
                if (!NetUtils.isNetWorkConnection()) {
                    UIUtils.showToast("请检查您的网络设置");
                    return;
                }
                //pLvMemberManager.resetHasMore();
                pLvMemberManager.setLoadmoreFinished(false);
                currentPage = 1;
                if (isPublicNews) {
                    netWork();
                } else {
                    netWorkNotify();
                }
            }
        });
        pLvMemberManager.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                currentPage++;
                if (isPublicNews) {
                    netWork();
                } else {
                    netWorkNotify();
                }
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        rl_underway.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doAllCanRead();
            }
        });
        rl_over.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doMessage();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (isPublicNews) {
                    Bundle bundle = new Bundle();
                    String token = SharedPreferencesUtils.getString(Constants.TOKEN, "");
                    bundle.putString(WebViewActivity.TITLE, "公告详情");
                    bundle.putString(WebViewActivity.URL, "http://hss.qianbaojiajia.com/earn/public" + Constants.TOKEN_TITLE + token + "&" + Constants.TOKEN_ID + publicNewsList.get(position).id);
                    ActivityUtils.startActivity(mContext, WebViewActivity.class, bundle);
                }
            }
        });
    }

    private void doMessage() {//消息
        currentPage = 1;
        publicNewsList.clear();
        listView.setAdapter(notifyAdapter);
        isPublicNews = false;
        loadingLayout.setStatus(LoadingLayout.Success);
        netWorkNotify();
        EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_NOTIFY_RED_HINT_SHOW));
        tv_title21.setTextColor(getResources().getColor(R.color.black3));
        iv_yi_shen_he.setVisibility(View.VISIBLE);
        iv_yi_shen_he.setTextColor(getResources().getColor(R.color.black3));
        tv_title11.setTextColor(getResources().getColor(R.color.yanse_newxian));
        iv_shen_he_zhong.setVisibility(View.GONE);
    }

    private void doAllCanRead() {//公告
        currentPage = 1;
        notifyList.clear();
        isPublicNews = true;
        listView.setAdapter(publicNewsAdapter);
        loadingLayout.setStatus(LoadingLayout.Success);
        netWork();
        tv_title11.setTextColor(getResources().getColor(R.color.black3));
        iv_shen_he_zhong.setVisibility(View.VISIBLE);
        iv_shen_he_zhong.setTextColor(getResources().getColor(R.color.black3));
        tv_title21.setTextColor(getResources().getColor(R.color.yanse_newxian));
        iv_yi_shen_he.setVisibility(View.GONE);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {


        }
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        super.onEvent(eventCode, data);
        switch (eventCode) {
            case EventCode.HSY_REQUEST_CODE_EXPIRE://登陆超时

                break;

        }
    }

    protected void netWorkNotify() {
        ApiManager.getApi().getNotifyList(
                "HSS001013",
                currentPage).enqueue(new NetCallBack<NotifyListBean>() {
            @Override
            protected void onSuccess(Call<NotifyListBean> call, Response<NotifyListBean> response) {
                if (currentPage == 1) {
                    notifyList.clear();
                }
                if (response != null && response.body() != null) {
                    NotifyListBean notifyListBean = response.body();
                    notifyList.addAll(notifyListBean.list);
                    if (notifyAdapter != null)
                        notifyAdapter.notifyDataSetChanged();
                    if (notifyList.size() == 0) {
                        loadingLayout.setStatus(LoadingLayout.Empty);
                    }
                    ApiManager.getApi().changeReadStates("HSS001014").enqueue(new NetCallBack<NotifyListBean>() {
                        @Override
                        protected void onSuccess(Call<NotifyListBean> call, Response<NotifyListBean> response) {

                        }

                    });
                } else {
                    UIUtils.showToast("暂无查询数据");
                    loadingLayout.setStatus(LoadingLayout.Error);
                }
            }

            @Override
            protected void hiddenProgress() {
                if (currentPage == 1) {
                    pLvMemberManager.finishRefresh();
                } else {
                    pLvMemberManager.finishLoadmore();
                }

            }

            @Override
            public void onFailure(Call<NotifyListBean> call, Throwable t) {
                super.onFailure(call, t);
                loadingLayout.setStatus(LoadingLayout.Error);
            }

        });

    }

    protected void netWork() {
        if (notifyAdapter != null)
            notifyAdapter.notifyDataSetChanged();
        ApiManager.getApi().getPublicNewsList(
                "HSS001015",
                currentPage,
                10
        ).enqueue(new NetCallBack<PublicNewsBean>() {
            @Override
            protected void onSuccess(Call<PublicNewsBean> call, Response<PublicNewsBean> response) {
                if (currentPage == 1) {
                    publicNewsList.clear();
                }
                if (response != null && response.body() != null) {
                    PublicNewsBean publicNewsBean = response.body();
                    publicNewsList.addAll(publicNewsBean.records);
                    if (publicNewsAdapter != null)
                        publicNewsAdapter.notifyDataSetChanged();
                    if (publicNewsList.size() == 0) {
                        loadingLayout.setStatus(LoadingLayout.Empty);
                    }
                } else {
                    UIUtils.showToast("暂无查询数据");
                    loadingLayout.setStatus(LoadingLayout.Error);
                }
            }

            @Override
            protected void hiddenProgress() {
                if (currentPage == 1) {
                    pLvMemberManager.finishRefresh();
                } else {
                    pLvMemberManager.finishLoadmore();
                }

            }

            @Override
            public void onFailure(Call<PublicNewsBean> call, Throwable t) {
                super.onFailure(call, t);
                loadingLayout.setStatus(LoadingLayout.Error);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
